//createRouter：用于创建vue-router实例对象
//RouteRecordRaw: 用于规范，路由规则，增加路由对象类型限制
//createWebHistory

import { createRouter, createWebHistory, RouteRecordRaw } from "vue-router"

//创建路由规则
const routes: Array<RouteRecordRaw> = [
  {
    path: "/",
    name: "index",
    component: () => import("@/page/index.vue"),
    redirect: "main",
    children: [
      {
        path: "/main",
        name: "main",
        component:()=> import("@/page/main/index.vue"),
        meta:{
          title:"首页"
        }
      },
      {
        path: "/columns",
        name: "columns",
        component: () => import("@/page/columns/index.vue"),
        meta:{
          title:"专栏"
        }
      },
      {
        path: "/book",
        name: "book",
        component: () => import("@/page/book/index.vue"),
        meta:{
          title:"书单"
        }
      },
      {
        path: "/note",
        name: "note",
        component: () => import("@/page/note/index.vue"),
        meta:{
          title:"日记"
        }
      },
      {
        path: "/comment",
        name: "comment",
        component: () => import("@/page/comment/index.vue"),
        meta:{
          title:"留言"
        }
      },
      {
        path: "/favorite",
        name: "favorite",
        component: () => import("@/page/favorite/index.vue"),
        meta:{
          title:"资源"
        }
      },
      {
        path: "/dynamic",
        name: "dynamic",
        component: () => import("@/page/dynamic/index.vue"),
        meta:{
          title:"动态"
        }
      },
      {
        path: "/about",
        name: "about",
        component: () => import("@/page/about/index.vue"),
        meta:{
          title:"关于"
        }
      },
      {
        path: "/link",
        name: "link",
        component: () => import("@/page/link/index.vue"),
        meta:{
          title:"友链"
        }
      },
    ]

  }
]

//创建路由实例
const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: routes
})


//路由导航守卫
// import { useUserStore } from "@/store/user"
router.beforeEach((to, _from, next) => {
  // const user = useUserStore()
  // const isLogin = user.token ? true : false
  // if (isLogin) {
  //   next()
  // } else {
  //   // router.push({ path: "/" })
  //   console.log("未登录")
  // }
  if (to.meta.title) {
    document.title = "King的博客 - " + to.meta.title as string
  }
  next()

})
export default router